<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.beaut.project.util.FlowUtility"%>
<%@ include file="/public/header.jsp"%>
<%
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
%>
<html>
	<head>
		<title>公共费用待办事项</title>
	<script type="text/javascript">
	jQuery(document).ready(function() {
		$("#company_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=8',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					doSearch();
				}
			});
		jQuery("#user_name").keypress(function(e) { 
	    	// 回车键事件 
	     	if(e.which == 13) { 
	     		 doSearch();
	       	} 
		});
		
		jQuery("#table").datagrid({
			title:"公共费用待办事项",
			iconCls:'icon-zc',
			loadMsg:'正在加载数据，请稍后......',
			url:'<%=contextPath %>/springMVC/finance/listPageFinanceCommons?status_flag=3&has_finished=0&is_all=1',
			rownumbers:true,
			fitColumns:true,
			singleSelect:true,
			nowrap:false,
			idField:'id',
			frozenColumns:[[
			      {field:'ck',checkbox:true},
			      {title:'主键',field:'id',hidden:true}
			]],
			columns:[[
				{title:'日期',field:'operation_time',width:100,align:'center'},
				{title:'申请人id',field:'user_id',width:100,align:'center',hidden:true},
				{title:'申请人',field:'user_name',width:100,align:'center'},
				{title:'所属公司',field:'name',width:100,align:'center'},
				{title:'金额',field:'money',width:100,align:'center'},
				{title:'摘要',field:'summary',width:200,align:'center'},
				{title:'确定承担部门',field:'is_all',width:100,align:'center',formatter:function(value,rowData,rowIndex){
					if(rowData.is_all == 0){
						return "确定";
					}
					return "不确定";
				}},
				{title:'查看凭证',field:'opMore',width:100,align:'center',formatter:function(value,rowData,rowIndex){
						var str="";
	                    	str+= "<input type='button' style='border:0px #ff0000 solid;width:16px;height:16px;cursor:pointer;' class='icon-details' title='详情' onclick=\"javascript:viewFC('"+rowData.id+"');\"/>";
	                    return str;
				}}
			]],
			pageSize:10,
			pageList : [10,50,100],  
			pagination:true,
			onLoadSuccess:function(rtJson){
				if(rtJson.rtState== '1')
				{
					$.messager.alert('提示',rtJson.rtMsrg);
				}
			}
		});
	});
	
	function viewFC(id){
		openDialogResize("<%=contextPath %>/springViews/finance/addOrEditFinanceCommons.jsp?callback=rtCallBack&id="+id,1000, 800);
	}
	function updateCommonsActualFinance(){
		var rows=jQuery('#table').datagrid('getSelections');
		var deleteVar="";
		var is_all="";
		var moneys=0;
		var user_ids="";
		var user_names="";
		if(rows.length==0)
		{
			jQuery.messager.alert('提示','选中要放款的报销单');  
			return ;
		}
		for(var i=0;i<rows.length;i++)
		{
			if(i==(rows.length-1))
			{
				deleteVar+=rows[i].id;
				is_all+=rows[i].is_all;
				user_ids+=rows[i].user_id;
				user_names+=rows[i].user_name;
			}else
			{
				deleteVar+=rows[i].id+",";
				is_all+=rows[i].is_all+",";
				user_ids+=rows[i].user_id+",";
				user_names+=rows[i].user_name+",";
			}
				moneys+= parseFloat(rows[i].money);
		}
		jQuery.messager.confirm('确认','您确认放款这些报销单吗？',function(msg){
			if(msg){
				jQuery('#dd').dialog({
			        title: '请选择公司账户对应的银行卡',
			        width: 200,
			        height: 67,
			        top:110,
			        closed: false,
			        cache: false,
			        href: '',
			        modal: true
			        });
				jQuery("#dd").show();
				
				jQuery('#bank_id').combogrid({
					idField:"id",
					textField:"account",
					title:'公司账户',
					panelWidth:550,
					panelHeight:200,
					rownumbers:true,
					url:'<%=contextPath %>/springMVC/finance/listPageFinanceCompany?flag=1',
					rownumbers:true,
					fitColumns:true,
					singleSelect:true,
					idField:'id',
					frozenColumns:[[
					    {title:'主键',field:'id',hidden:true}
					]],
					columns:[[
						{title:'银行',field:'bank',width:200,align:'center'},
						{title:'卡号',field:'account',width:200,align:'center'},
						{title:'开户人',field:'owner',width:100,align:'center'},
						{title:'金额',field:'money',width:100,align:'center'}
					]],
					pageSize:10,
					pageList : [10,50,100],  
					pagination:true,
					toolbar:'#tb1',
					onChange:function(){
						var row = jQuery('#bank_id').combogrid("grid").datagrid('getSelected');
						jQuery('#bank').val(row.bank);
						jQuery('#finance_company_id').val(row.id);
						jQuery('#account').val(row.account);
						jQuery('#money').val(row.money);
						showDilog(deleteVar,moneys,rows,is_all,user_ids,user_names);
					}
				});
			}
		});
	}
	function showDilog(deleteVar,moneys,rows,is_all,user_ids,user_names){
		var money=jQuery('#money').val();
		var bank=jQuery('#bank').val();
		var finance_company_id=jQuery('#finance_company_id').val();
		var account=jQuery('#account').val();
		if(parseFloat(money)>parseFloat(moneys)){
		if(money==0){
			jQuery.messager.alert('提示','请先选择使用的公司账户(银行卡)');  
			return false;
		}else{
			jQuery('#dd').dialog('close'); 
			jQuery.messager.confirm('确认','此公司账户(银行卡)</br>'+bank+'</br>卡号：'+account+'支出金额('+moneys+')',function(msg){
				if(msg){
					jQuery.ajax({
							type:'POST',
							url:"<%=contextPath %>/springMVC/finance/updateFianceActualLoan",
							data:"ids="+deleteVar+"&money="+moneys+"&finance_company_id="+finance_company_id+"&is_all="+is_all+"&user_ids="+user_ids+"&user_names="+user_names,
							dataType:"json",
							success:function(rtJson){
								if(rtJson.rtState == '0'){
									warningInfo(rtJson.rtMsrg);
									table_reload(rows.length);
									
								}
								else{
								jQuery.messager.alert('提示：',rtJson.rtMsrg);
								}
							}
						});
						}
			}
			)}
		}else{
			jQuery.messager.alert('提示','此公司账户(银行卡)</br>'+bank+'</br>卡号：'+account+'</br>金额('+money+')不足以支付。请选择其他账户（银行卡）');  
			jQuery('#dd').dialog('close'); 
		}
	}
	function rtCallBack(){
		$('#table').datagrid('reload');
	}
		
	
		function doSearch() {
			var start_time=jQuery('#start_time').datebox('getValue');  
			var end_time=jQuery('#end_time').datebox('getValue'); 
			var user_name=jQuery('#user_name').val();
			var company_id = $('#company_id').combotree('getValue');
			jQuery('#table').datagrid('load', {
				start_time:start_time,
				end_time:end_time,
				user_name:user_name,
				company_id:company_id
			});
		}
	</script>
	</head>
	<body style="height:97%">
			<div id="table"  data-options="toolbar:'#tb',fit:true"></div>  
				<div id="tb" style="padding:3px;height:auto">
				<div style="float:left;">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="updateCommonsActualFinance();">放款</a>
			</div>
			<div align="right">
				开始时间：<input id="start_time" name="start_time" class="easyui-datebox" style="width:100px" />&nbsp;&nbsp;&nbsp;结束时间：<input id="end_time" name="end_time" class="easyui-datebox" style="width:100px"/>
				申请人：<input type="text" id="user_name" name="user_name"   style="width:100px">
				所属公司：<input type="text" id="company_id" name="company_id"   style="width:100px">
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="doSearch();"></a>
			</div>
		</div>
		<div id="dd" >
				<div style="text-align:center;">
					<input type="text" id="bank_id" name="bank_id"  data-options="required:true">
				</div>
			</div>
				<input type="hidden" id="bank" name="bank" />
				<input type="hidden" id="finance_company_id" name="finance_company_id" />
				<input type="hidden" id="account" name="account" />	
				<input type="hidden" id="money" name="money" value="0" />	
	</body>
</html>